//
//  SetUserGenderView.swift
//  IdleFish
//
//  Created by Ben on 2022/1/3.
//

import SwiftUI

struct SetUserGenderView: View {
    
    @EnvironmentObject var userDataVM: UserDataViewModel
    
    var body: some View {
        
        GeometryReader { proxy in
            
            VStack(spacing: 0) {
                Spacer()
                
                HStack {
                    Button {
                        userDataVM.showGenderPicker.toggle()
                    } label: {
                        Text("取消")
                            .foregroundColor(.black)
                    }
                    
                    Spacer()
                    
                    Button {
                        Task {
                            await userDataVM.updateUserGender()
                        }
                    } label: {
                        Text("确定")
                            .foregroundColor(.black)
                    }
                }
                .frame(height: 40)
                .padding(.horizontal)
                .background(Color.yellow)
                .zIndex(1)
                
                // 性别选择器， userDataVM.selectedGender用来记录当前选择的性别
                Picker("", selection: $userDataVM.selectedGender) {
                    ForEach(UserGender.allCases) {
                        // 展示每一个选项，特别需要注意的是tag是真正用来通知$userDataVM.selectedGender的关键
                        Text($0.value).tag($0)
                    }
                }
                .pickerStyle(.inline)
                .frame(height: 100)
                .padding(.bottom, SCREEN_SAFE_INSETS.bottom)
                .background(Color.white)
            }
        }
        .frame(height: SCREEN_HEIGHT)
        .background(Color.black.opacity(0.4))
    }
}

struct SetUserGenderView_Previews: PreviewProvider {
    static var previews: some View {
        SetUserGenderView()
    }
}
